"use strict";
cc._RF.push(module, 'f0506DVS8xNYow3MJfOEyk3', 'JoystickLeft');
// script/game/JoystickLeft.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Constants_1 = require("../libs/Constants");
var HelperMgr_1 = require("../libs/HelperMgr");
var GameManager_1 = require("./GameManager");
var Ground_1 = require("./Ground");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var JoystickLeft = /** @class */ (function (_super) {
    __extends(JoystickLeft, _super);
    function JoystickLeft() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.player = null;
        _this.dot = null;
        _this.ring = null;
        _this.curDir = "";
        _this.faceTo = "K";
        _this.moveDir = new cc.Vec2(0, 1);
        _this._speedType = Constants_1.default.SpeedType.STOP;
        _this.stopSpeed = 0;
        _this.normalSpeed = 200;
        _this.fastSpeed = 200;
        _this.joystickType = Constants_1.default.JoystickType.FOLLOW;
        _this.directionType = Constants_1.default.DirectionType.FOUR;
        _this._stickPos = null;
        _this._touchLocation = null;
        _this._radius = null;
        // from self
        _this._moveSpeed = 0;
        _this._movePosi = null;
        //playe getPosition
        _this.playerPos = null;
        _this._isMove = false;
        return _this;
    }
    JoystickLeft.prototype.onLoad = function () {
        this.player = GameManager_1.default.instace.player;
        this._radius = this.ring.width / 2;
        this._initTouchEvent();
        if (this.joystickType === Constants_1.default.JoystickType.FOLLOW) {
            this.node.opacity = 0;
        }
    };
    JoystickLeft.prototype._initTouchEvent = function () {
        this.node.on(cc.Node.EventType.TOUCH_START, this._touchStartEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._touchMoveEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._touchEndEvent, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._touchEndEvent, this);
    };
    JoystickLeft.prototype._touchStartEvent = function (event) {
        var touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
        if (this.joystickType === Constants_1.default.JoystickType.FIXED) {
            this._stickPos = this.ring.getPosition();
            // 触摸点与圆圈中心的距离
            var distance = touchPos.sub(this.ring.getPosition()).mag();
            // 手指在圆圈内触摸,控杆跟随触摸点
            this._radius > distance && this.dot.setPosition(touchPos);
        }
        else if (this.joystickType === Constants_1.default.JoystickType.FOLLOW) {
            // 记录摇杆位置，给 touch move 使用
            this._stickPos = touchPos;
            this.node.opacity = 255;
            this._touchLocation = event.getLocation();
            // 更改摇杆的位置
            this.ring.setPosition(touchPos);
            this.dot.setPosition(touchPos);
        }
        this._isMove = true;
    };
    JoystickLeft.prototype._touchMoveEvent = function (event) {
        // 如果 touch start 位置和 touch move 相同，禁止移动
        if (this.joystickType === Constants_1.default.JoystickType.FOLLOW && this._touchLocation === event.getLocation()) {
            return false;
        }
        // 以圆圈为锚点获取触摸坐标
        var touchPos = this.ring.convertToNodeSpaceAR(event.getLocation());
        var distance = touchPos.mag();
        // 由于摇杆的 postion 是以父节点为锚点，所以定位要加上 touch start 时的位置
        var posX = this._stickPos.x + touchPos.x;
        var posY = this._stickPos.y + touchPos.y;
        // 归一化
        var p = cc.v2(posX, posY).sub(this.ring.getPosition()).normalize();
        var speedType;
        if (this._radius > distance) {
            this.dot.setPosition(cc.v2(posX, posY));
            speedType = Constants_1.default.SpeedType.NORMAL;
        }
        else {
            // 控杆永远保持在圈内，并在圈内跟随触摸更新角度
            var x = this._stickPos.x + p.x * this._radius;
            var y = this._stickPos.y + p.y * this._radius;
            this.dot.setPosition(cc.v2(x, y));
            speedType = Constants_1.default.SpeedType.FAST;
        }
        this.onTouchMove(event, { speedType: speedType, moveDistance: p });
    };
    JoystickLeft.prototype._touchEndEvent = function (event) {
        this.dot.setPosition(this.ring.getPosition());
        if (this.joystickType === Constants_1.default.JoystickType.FOLLOW) {
            this.node.opacity = 0;
        }
        this.onTouchEnd(event, { speedType: Constants_1.default.SpeedType.STOP });
        this._isMove = false;
    };
    JoystickLeft.prototype.move = function () {
        var newPos = this.getfwinfo(this.moveDir);
        var nowmovespeed = this._moveSpeed / 120;
        var p = this.player.getPosition();
        switch (newPos) {
            case 'W':
                p.y += nowmovespeed;
                break;
            case 'S':
                p.y -= nowmovespeed;
                break;
            case 'A':
                p.x -= nowmovespeed;
                break;
            case 'D':
                p.x += nowmovespeed;
                break;
            case 'WA':
                p.y += nowmovespeed;
                p.x -= nowmovespeed;
                break;
            case 'SA':
                p.y -= nowmovespeed;
                p.x -= nowmovespeed;
                break;
            case 'WD':
                p.y += nowmovespeed;
                p.x += nowmovespeed;
                break;
            case 'SD':
                p.y -= nowmovespeed;
                p.x += nowmovespeed;
                break;
        }
        if (HelperMgr_1.default.PointInPoly(p, GameManager_1.default.instace.ground.getComponent(Ground_1.default).points)) {
            this.player.setPosition(p);
        }
    };
    JoystickLeft.prototype.onTouchMove = function (event, data) {
        this._speedType = data.speedType;
        this.moveDir = data.moveDistance;
        //获取移动方位
        var newPos = this.getfwinfo(this.moveDir);
        // console.log(newPos);
        //判断移动方位跟之前的方位是否发生变化，如果发生变化，切切换动画
        if (this.faceTo != newPos) {
            switch (newPos) {
                case 'K':
                // this.toStand(); break;
                case 'W':
                // this.toWalk(1); break;
                case 'S':
                // this.toWalk(5); break;
                case 'A':
                // this.toWalk(7); break;
                case 'D':
                // this.toWalk(3); break;
                case 'WA':
                // this.toWalk(0); break;
                case 'SA':
                // this.toWalk(6); break;
                case 'WD':
                // this.toWalk(2); break;
                case 'SD':
                // this.toWalk(4); break;
            }
        }
    };
    //播放移动动画
    JoystickLeft.prototype.toWalk = function (dir) {
        if (dir == this.curDir)
            return;
    };
    JoystickLeft.prototype.update = function (dt) {
        switch (this._speedType) {
            case Constants_1.default.SpeedType.STOP:
                this._moveSpeed = this.stopSpeed;
                break;
            case Constants_1.default.SpeedType.NORMAL:
                this._moveSpeed = this.normalSpeed;
                break;
            case Constants_1.default.SpeedType.FAST:
                this._moveSpeed = this.fastSpeed;
                break;
            default:
                break;
        }
        this.move();
    };
    JoystickLeft.prototype.onTouchEnd = function (event, data) {
        this._speedType = data.speedType;
        if (event.type == "touchcancel" && this._movePosi) {
            this.player.setPosition(this._movePosi);
        }
    };
    //获取方位信息
    JoystickLeft.prototype.getfwinfo = function (data) {
        var CurrentDire = 'K';
        var currentAngle = this.getCalculaAngle(data);
        if (currentAngle <= 22.5 && currentAngle >= 0 || currentAngle <= 360 && currentAngle >= 337.5) //0;左
            CurrentDire = "A";
        else if (currentAngle <= 67.5 && currentAngle >= 22.5) //45;左上
            CurrentDire = "WA";
        else if (currentAngle <= 112.5 && currentAngle >= 67.5) //90;上
            CurrentDire = "W";
        else if (currentAngle <= 157.5 && currentAngle >= 112.5) //135;右上
            CurrentDire = "WD";
        else if (currentAngle <= 202.5 && currentAngle >= 157.5) //180;右
            CurrentDire = "D";
        else if (currentAngle <= 247.5 && currentAngle >= 202.5) //225;右下
            CurrentDire = "SD";
        else if (currentAngle <= 292.5 && currentAngle >= 247.5) //270;下
            CurrentDire = "S";
        else if (currentAngle <= 337.5 && currentAngle >= 292.5) //315;左下
            CurrentDire = "SA";
        return CurrentDire;
    };
    //角度转换180
    JoystickLeft.prototype.getCalculaAngle = function (data) {
        var currentAngleX = data.x * 90 + 90; //X轴 当前角度
        var currentAngleY = data.y * 90 + 90; //Y轴 当前角度
        if (currentAngleY < 90) {
            if (currentAngleX < 90) {
                return 270 + currentAngleY;
            }
            else if (currentAngleX > 90) {
                return 180 + (90 - currentAngleY);
            }
        }
        return currentAngleX;
    };
    __decorate([
        property({
            type: cc.Node,
            displayName: 'Dot',
            tooltip: '摇杆操纵点',
        })
    ], JoystickLeft.prototype, "dot", void 0);
    __decorate([
        property({
            type: cc.Node,
            displayName: 'Ring',
            tooltip: '摇杆背景节点',
        })
    ], JoystickLeft.prototype, "ring", void 0);
    __decorate([
        property({
            tooltip: 'curDir',
        })
    ], JoystickLeft.prototype, "curDir", void 0);
    __decorate([
        property({
            tooltip: 'faceTo',
        })
    ], JoystickLeft.prototype, "faceTo", void 0);
    __decorate([
        property({
            displayName: 'Move Dir',
            tooltip: '移动方向',
        })
    ], JoystickLeft.prototype, "moveDir", void 0);
    __decorate([
        property({
            type: cc.Integer,
            tooltip: '停止时速度',
        })
    ], JoystickLeft.prototype, "stopSpeed", void 0);
    __decorate([
        property({
            type: cc.Integer,
            tooltip: '正常速度',
        })
    ], JoystickLeft.prototype, "normalSpeed", void 0);
    __decorate([
        property({
            type: cc.Integer,
            tooltip: '最快速度',
        })
    ], JoystickLeft.prototype, "fastSpeed", void 0);
    __decorate([
        property({
            type: cc.Enum(Constants_1.default.JoystickType),
            displayName: 'joystickType',
            tooltip: 'Touch Type',
        })
    ], JoystickLeft.prototype, "joystickType", void 0);
    __decorate([
        property({
            type: cc.Enum(Constants_1.default.DirectionType),
            displayName: 'directionType',
            tooltip: '方向类型',
        })
    ], JoystickLeft.prototype, "directionType", void 0);
    __decorate([
        property({
            tooltip: '摇杆所在位置',
        })
    ], JoystickLeft.prototype, "_stickPos", void 0);
    __decorate([
        property({
            tooltip: '触摸位置',
        })
    ], JoystickLeft.prototype, "_touchLocation", void 0);
    JoystickLeft = __decorate([
        ccclass
    ], JoystickLeft);
    return JoystickLeft;
}(cc.Component));
exports.default = JoystickLeft;

cc._RF.pop();